﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <!--SEO优化部分 & 全局样式部分-->
    @Html.Partial("_Title")
    <script src="/Theme/layui/layui.js"></script>
    <script src="/Theme/layer/layer.js"></script>
    <link href="~/Theme/MyCSS/EditPopup.css" rel="stylesheet" />
</head>
<body class="">

    <div style="overflow:hidden;  display:none" id="EditTitlecontentid">
        <!-- 内容展示区域 -->
        <div style="padding-left: 10px; padding-right: 10px;">
            <table lay-filter="parse-table-demo2">
                <thead>
                    <tr>
                        <th lay-data="{field:'xuhao', width:40}">序号</th>
                        <th lay-data="{field:'subject', width:350}">产品标题</th>
                        <th lay-data="{field:'UpdateSubject', width:350}">修改后的产品标题</th>
                    </tr>
                </thead>
                <tbody id="EditTitleProlist"></tbody>
            </table>
        </div>
        <!-- 操作区 -->
        <div class="title-rules">
            <div class="mt10">
                <label class="v-align-m">
                    <input class="checkbox" type="checkbox" checked="checked" name="modifyTitle" value="" id="title-rules-1">
                    <span class="label">查找并替换：</span>
                </label>
                <input type="text" value="" class="input" id="rules-1-input-1">
                <span class="label">替换为</span>
                <input type="text" value="" class="input" id="rules-1-input-2">
            </div>
            <div class="mt10">
                <label class="v-align-m">
                    <input class="checkbox" type="checkbox" name="modifyTitle" value="" id="title-rules-2">
                    <span class="label">添加前后缀：</span>
                </label>
                <input type="text" value="" class="input" id="rules-2-input-1">
                <span class="label">+产品标题+</span>
                <input type="text" value="" class="input" id="rules-2-input-2">
            </div>
            <div class="mt10">
                <label class="v-align-m">
                    <input class="checkbox" type="checkbox" name="modifyTitle" value="" id="title-rules-3">
                    <span class="label">删除关键词：</span>
                </label>
                <input type="text" value="" class="input" id="rules-3-input-1">
            </div>
        </div>
        <div class="Update-but">
            <button class="layui-btn layui-btn-warm layui-btn-small" onclick="previewUpadate()">预览修改</button>
            <button class="layui-btn layui-btn-small layui-btn-normal" onclick="SubmitUpdate()">保存修改</button>
        </div>  
    </div>
    <script>
    @*//动态加载CSS或JS。
        //dynamicLoading.css("/Theme/MyCSS/EditPopup.css");



        //动态加载CSS或JS。
        var dynamicLoading = {
            css: function (path) {
                if (!path || path.length === 0) {
                    throw new Error('argument "path" is required !');
                }
                var head = document.getElementsByTagName('head')[0];
                var link = document.createElement('link');
                link.href = path;
                link.rel = 'stylesheet';
                link.type = 'text/css';
                head.appendChild(link);
            },
            js: function (path) {
                if (!path || path.length === 0) {
                    throw new Error('argument "path" is required !');
                }
                var head = document.getElementsByTagName('head')[0];
                var script = document.createElement('script');
                script.src = path;
                script.type = 'text/javascript';
                head.appendChild(script);
            }
        }*@

</script>

    <script>

        //页面初始化
        $(function () {
            BindPorducts();
        })

        //绑定要编辑的产品集合
        var BindPorducts = function () {

            //获取用户要批量修改的产品值集合
            var Products = parent.newallproducts

            var htmls = "";
            var xh = 0;
            for (var i = 0; i < Products.length; i++) {
                xh = xh + 1;
                htmls += "<tr>"
                htmls += "<td>" + xh + "</td>";
                htmls += "<td>" + Products[i]["subject"] + "</td>";
                htmls += "<td></td>";
                htmls += "</tr>"
            }

            //插入htnl代码
            $("#EditTitleProlist").html(htmls);

            //渲染表格
            layui.use('table', function () {
                var table = layui.table;
                table.init('parse-table-demo2', { //转化静态表格
                    height: '250'
                });

                //避免屏幕闪动，在Layui脚本渲染后显示表格区域
                $("#EditTitlecontentid").show();

            });
        }

    </script>
    <script>

        //预览修改
        var previewUpadate = function () {
            //判断
            if ($('#title-rules-1').is(':checked')) {
                var parameter1 = $('#rules-1-input-1').val().trim();
                if (parameter1 == "") { layer.alert("查找的内容不能为空"); return; }
            }

            //循环产品标题列表
            $("table.layui-table tbody tr").each(function () {              
                var title = $(this).find("td").eq(1).find("div").text();    
                var newtitle = replaceTitle(title);
                $(this).find("td").eq(2).find("div").html(newtitle);
            })
           


           
            

           

        }
        //替换标题
        var replaceTitle = function (title) {
            var titlestring = title;
            var judgeTitlesting = title;

            var isSelect = false;
            var isUpdate = false;
            //第一操作组
            if ($('#title-rules-1').is(':checked')) {
                isSelect = true;
                var parameter1 = $('#rules-1-input-1').val().trim();
                var parameter2 = $('#rules-1-input-2').val().trim();
                if (parameter2 != ""){
                    //标题是否包含parameter1
                    if (judgeTitlesting.indexOf(parameter1) != -1) {
                        isUpdate = true;

                        judgeTitlesting = judgeTitlesting.replace(parameter1, parameter2);

                        if (getByteLen(judgeTitlesting) > 60) { return "<span class='Highlight'>标题字数超过30个字(60个字符)！</span>" }

                        titlestring = titlestring.replace(parameter1, "<span class='Highlight'>" + parameter2 + "</span>");
                        
                    }
                }
                else {
                    //标题是否包含parameter1
                    if (judgeTitlesting.indexOf(parameter1) != -1) {
                        isUpdate = true;

                        judgeTitlesting = judgeTitlesting.replace(parameter1, parameter2);
                        titlestring = titlestring.replace(parameter1, parameter2);
                    }                   
                }
                
            }
            //第二操作组
            if ($('#title-rules-2').is(':checked')) {
                isSelect = true;
                var parameter1 = $('#rules-2-input-1').val().trim();
                var parameter2 = $('#rules-2-input-2').val().trim();

                if (parameter1 != "") {
                    isUpdate = true;

                    judgeTitlesting = judgeTitlesting + parameter1;

                    if (getByteLen(judgeTitlesting) > 60) { return "<span class='Highlight'>标题字数超过30个字(60个字符)！</span>" }

                    titlestring = "<span class='Highlight'>" + parameter1 + "</span>" + titlestring;
                }
                if (parameter2 != "") {
                    isUpdate = true;

                    judgeTitlesting = judgeTitlesting + parameter2;
                    if (getByteLen(judgeTitlesting) > 60) { return "<span class='Highlight'>标题字数超过30个字(60个字符)！</span>" }

                    titlestring = titlestring + "<span class='Highlight'>" + parameter2 + "</span>";
                }
            }
            //第三操作组
            if ($('#title-rules-3').is(':checked')) {
                isSelect = true;
                var parameter1 = $('#rules-3-input-1').val().trim();
                if (parameter1 != "") {
                    //标题是否包含parameter1
                    if (judgeTitlesting.indexOf(parameter1) != -1) {
                        isUpdate = true;
                        titlestring = titlestring.replace(parameter1, "");
                    }
                };
            }
            if (!isSelect) { return "<span class='Highlight'>未输入操作类型</span>" }
            if (isUpdate) {
                return titlestring;
            }
            else { return "<span class='Highlight'>未找到匹配项</span>" }
        }


        //获取字符串长度（汉字算两个字符，字母数字算一个）
        var getByteLen = function(val) {
            var len = 0;
            for (var i = 0; i < val.length; i++) {
                var a = val.charAt(i);
                if (a.match(/[^\x00-\xff]/ig) != null) {
                    len += 2;
                }
                else {
                    len += 1;
                }
            }
            return len;
        }


        var submitProducts = new Array();
        var index3;
        //保存修改
        var SubmitUpdate = function ()
        {
            //获取用户要批量修改的产品值集合
            var Products = parent.newallproducts
            
            var proObj = new Object();
            for (var i = 0; i < Products.length; i++) {
                var newTitle = replaceSubmitTitle(Products[i]["subject"]);
                if (newTitle != "") {
                    proObj = new Object();
                    proObj["offerId"] = Products[i]["offerId"];
                    proObj["subject"] = newTitle;
                    submitProducts.push(proObj);
                }
            }           
            //var productsJson = JSON.stringify(submitProducts)
            //layer.alert(productsJson); 

            //执行循环提交。
            AjaxPostProTitle()

            //获取总数量
            zsCount = submitProducts.length;

            var layerHtmls = "<div class='layui-progress layui-progress-big' lay-showpercent='true' lay-filter='demo'><div id='cccccc'  class='layui-progress-bar layui-bg-red' lay-percent='0 / 0'></div></div>"
            index3 = layer.open({
                type: 1,
                title: "保存修改进度",
                area: ['420px', '180px'], //宽高
                content: layerHtmls,
                success: function () {
                    layui.use('element', function () {
                    });
                }
            });

           
        }

        var SuccessCount = 0;
        var failCount = 0;
        var zsCount = 0;
        var AjaxPostProTitle = function ()
        {
            if (submitProducts.length > 0) {

                var obj = submitProducts.shift();
                $.ajax({
                    url: "/api/TitleUpdate/UpdateTitl",
                    type: "Post",
                    dataType: "json",
                    data: { offerId: obj["offerId"], subject: obj["subject"] },
                    success: function (data) {
                        if (data) {

                            SuccessCount++;                           
                            AjaxPostProTitle();
                        }
                        else {
                            failCount++;
                        }

                        $("#cccccc").css("width", "" + (Math.round(SuccessCount / zsCount * 10000) / 100 + "%") + "")

                        $("#cccccc").find("span").text("" + SuccessCount + " / " + zsCount + "");
                    }
                });
            }
            else {
                layer.close(index3);
                layer.alert("操作成功:（" + SuccessCount + "）条,失败：（" + failCount + "）条")
                //parent.layer.close(3);
            }
        }

        //替换提交标题
        var replaceSubmitTitle = function (title) {

            var titlestring = title;

            var is1 = $('#title-rules-1').is(':checked')
            var parameter1 = $('#rules-1-input-1').val().trim();
            var parameter2 = $('#rules-1-input-2').val().trim();

            var is2 = $('#title-rules-2').is(':checked')
            var parameter3 = $('#rules-2-input-1').val().trim();
            var parameter4 = $('#rules-2-input-2').val().trim();

            var is3 = $('#title-rules-3').is(':checked')
            var parameter5 = $('#rules-3-input-1').val().trim();

            var isSelect = false;
            var isUpdate = false;
          

            //第一操作组
            if (is1) {
                if (parameter1 == "") { return ""; };
                isSelect = true;
               
                if (parameter2 != "") {
                    //标题是否包含parameter1
                    if (titlestring.indexOf(parameter1) != -1) {
                        isUpdate = true;

                        var titlestr = titlestring.replace(parameter1, parameter2)
                        if (getByteLen(titlestr) > 60) { return "" }

                        titlestring = titlestring.replace(parameter1, parameter2);
                    }
                }
                else {
                    //标题是否包含parameter1
                    if (titlestring.indexOf(parameter1) != -1) {
                        isUpdate = true;
                        titlestring = titlestring.replace(parameter1, parameter2);
                    }
                }
            }
            //第二操作组
            if (is2) {
                isSelect = true;
                
                if (parameter3 != "") {
                    isUpdate = true;

                    var titlestr = titlestring + parameter3;
                    if (getByteLen(titlestr) > 60) { return "" }

                    titlestring =  parameter3+ titlestring;
                }
                if (parameter4 != "") {
                    isUpdate = true;

                    var titlestr = titlestring + parameter4;
                    if (getByteLen(titlestr) > 60) { return "" }

                    titlestring = titlestring + parameter4;
                }
            }
            //第三操作组
            if (is3) {
                isSelect = true;
               
                if (parameter5 != "") {
                    //标题是否包含parameter1
                    if (titlestring.indexOf(parameter5) != -1) {
                        isUpdate = true;
                        titlestring = titlestring.replace(parameter5, "");
                    }
                };
            }
            if (!isSelect) { return "" }
            if (isUpdate) {
                return titlestring;
            }
            else { return "" }
        }

    </script>


    </body>
    </html>

